﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OGS.Contracts;
using OGS.Utility;
using OGS.Web.Models.Admin;

namespace OGS.Web.Controllers.Admin
{
    public partial class AdminController : AdminBaseController
    {
        /// <summary>
        /// Save Product Action Result
        /// </summary>
        /// <returns></returns>
        [ActionName("Save-Product")]
        public ActionResult SaveProduct(int? ID)
        {
            SaveProductModel SaveProductModel = new SaveProductModel();
            SaveProductModel.IsActive = true;
            if (ID.HasValue)
            {
                //Get Product from DB
                SaveProductModel.Populate(ID.Value);

                SaveProductModel.IsSuccess = Session["Success"] == null ? false : true;
                Session["Success"] = null;

            }

            SaveProductModel.PopulateParentCategories();
            SaveProductModel.ParentCategories.Insert(0, new Category { ID = 0, ComputedName = "--- Select ---" });

            SaveProductModel.PopulateBrands();
            SaveProductModel.Brands.Insert(0, new Brand { ID = 0, Name = "--- Select ---" });

            SaveProductModel.PopulateUOMs();
            SaveProductModel.UOMs.Insert(0, new UOM { ID = 0, Name = "--- Select ---" });

            //Populate Child Categories
            if (SaveProductModel.ParentCategoryID > 0)
            {
                SaveProductModel.PopulateChildCategories(SaveProductModel.ParentCategoryID);
            }

            return View("~/Views/Admin/Product/SaveProduct.aspx", SaveProductModel);
        }

        /// <summary>
        /// Save Product in DB
        /// </summary>
        /// <param name="oSaveProductModel"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [ActionName("Save-Product")]
        public ActionResult SaveProduct(SaveProductModel SaveProductModel)
        {
            if (ModelState.IsValid)
            {
                string ImageName = DateTime.Now.Ticks.ToString() + ".jpg";
                if (SaveProductModel.ProductImage != null)
                {
                    SaveProductModel.ProductImage.SaveAs(Server.MapPath("/UploadedFiles/ProductImages/") + ImageName);
                    SaveProductModel.Image = ImageName;
                }
                if (SaveProductModel.ProductBigImage != null)
                {
                    SaveProductModel.ProductBigImage.SaveAs(Server.MapPath("/UploadedFiles/ProductImages/BigImages/") + ImageName);
                    SaveProductModel.Image_Big = ImageName;
                }

                Result result = (Result)SaveProductModel.SaveProduct();

                if (result == Result.Success)
                {
                    Session["Success"] = true;
                    return Redirect("/admin/save-Product/" + SaveProductModel.ID.ToString());
                }
                else if (result == Result.Duplicate)
                {
                    ModelState.AddModelError("EnglishName", "The name already exists.");
                }
                else
                {
                    ModelState.AddModelError("EnglishName", "Some error occurred. Please try again later.");
                }

            }

            SaveProductModel.PopulateParentCategories();
            SaveProductModel.ParentCategories.Insert(0, new Category { ID = 0, ComputedName = "--- Select ---" });

            SaveProductModel.PopulateBrands();
            SaveProductModel.Brands.Insert(0, new Brand { ID = 0, Name = "--- Select ---" });

            SaveProductModel.PopulateUOMs();
            SaveProductModel.UOMs.Insert(0, new UOM { ID = 0, Name = "--- Select ---" });

            //Populate Child Categories
            if (SaveProductModel.ParentCategoryID > 0)
            {
                SaveProductModel.PopulateChildCategories(SaveProductModel.ParentCategoryID);
            }


            return View("~/Views/Admin/Product/SaveProduct.aspx", SaveProductModel);
        }

        /// <summary>
        /// Product List Action
        /// </summary>
        /// <returns></returns>
        [ActionName("Product-List")]
        public ActionResult ProductList()
        {
            SearchAttributes SearchAttributes = GetSearchAttributesFromQuesryString();

            string Name = Helper.GetQueryStringValue("Name");
            int ParentCategoryID = Helper.GetQueryStringValueInt("ParentCategory");
            int CategoryID = Helper.GetQueryStringValueInt("Category");
            int BrandID = Helper.GetQueryStringValueInt("Brand");

            ProductListModel ProductListModel = new ProductListModel();
            ProductListModel.Populate(Name, BrandID, ParentCategoryID, CategoryID, SearchAttributes);
            ProductListModel.SearchAttributes = SearchAttributes;

            return View("~/Views/Admin/Product/ProductList.aspx", ProductListModel);
        }

        /// <summary>
        /// Returns child categories as json format
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public ActionResult GetChildCategories(int? ID)
        {
            string response = string.Empty;
            SaveProductModel SaveProductModel = new SaveProductModel();
            if (ID.HasValue && ID.Value > 0)
            {
                SaveProductModel.PopulateChildCategories(ID.Value);
            }

            return Json(SaveProductModel.Categories, JsonRequestBehavior.AllowGet);
        }
    }
}
